//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.order;

import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import org.jpgolay.me.agency.Agency;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.salesperson.SalesPerson;

// Referenced classes of package org.jpgolay.me.order:
//            NewOrderGUI
public class NewOrderController {

    public NewOrderController(JDesktopPane desktop) {
        this.desktop = desktop;
        initComponent();
    }

    private void initComponent() {
        if (noGui == null) {
            noGui = new NewOrderGUI();
            initList();
            noGui.setLocation(100, 100);
        }
        init();
    }

    private void initList() {
        crud = new ObjectCRUD();
        String l[] = (String[]) null;
        java.util.Collection list = crud.readAll(null, 15);
        if (list != null) {
            salesPersonList = new ArrayList(list);
            l = new String[salesPersonList.size()];
            Iterator it = salesPersonList.iterator();
            for (int i = 0; it.hasNext(); i++) {
                salesPerson = (SalesPerson) it.next();
                l[i] = salesPerson.getNom() + "  " + salesPerson.getPrenom();
            }

        }
        noGui.setSalespersonList(l);
        noGui.setSelectedIndexSalesperson(0);
        list = crud.readAll(null, 21);
        if (list != null) {
            agencyList = new ArrayList(list);
        }
    }

    public void actionOK() {
        agency = (Agency) agencyList.get(noGui.getSelectedAgency());
        salesPerson = (SalesPerson) salesPersonList.get(noGui.getSelectedSalesperson());
        typePiece = noGui.getTypePiece();
        actionCANCEL();
    }

    public void actionCANCEL() {
        noGui.setVisible(false);
        desktop.remove(noGui);
    }

    public int getTypePiece() {
        return typePiece;
    }

    public Agency getAgency() {
        return agency;
    }

    public SalesPerson getSalesPerson() {
        return salesPerson;
    }

    public void init() {
        noGui.setVisible(true);
        noGui.toFront();
        desktop.add(noGui, JLayeredPane.DEFAULT_LAYER);
        try {
            noGui.setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public JButton getOK() {
        return noGui.getOKButton();
    }

    public JButton getCANCEL() {
        return noGui.getCANCELButton();
    }
    private JDesktopPane desktop;
    private NewOrderGUI noGui;
    private ObjectCRUD crud;
    private ArrayList salesPersonList;
    private ArrayList agencyList;
    private SalesPerson salesPerson;
    private Agency agency;
    private int typePiece;
}
